<?php

class DefaultController extends FController
{
	public $layout='//layouts/main';
	public $subTmp = 0;
	public function init(){
		Yii::app()->theme = 'frontend';
		parent::init();
	}
	
	public function actionIndex()
	{
	/*Begin tutorial*/
		$criteria=new CDbCriteria;
		$criteria->together = false;
		$criteria->order = 'sub_category_id';	
		$criteria->with = array(
			'category',
			'sub_category',
			'users',
		);		
		$dataProvider=new CActiveDataProvider('Article', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageVar'=>'page',
			),
		));
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			
		));
		/*End tutorial*/
	}
	public function actionSignup()
	{
	/*Begin tutorial*/
		$criteria=new CDbCriteria;
		$criteria->together = false;
		$criteria->order = 'sub_category_id';	
		$criteria->with = array(
			'category',
			'sub_category',
			'users',
		);		
		$dataProvider=new CActiveDataProvider('Article', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageVar'=>'page',
			),
		));
		$this->render('signup', array(
			'dataProvider'=>$dataProvider,
			
		));
		/*End tutorial*/
	}
	public function actionCategory($alias = null)
	{
		$criteria=new CDbCriteria;
		$criteria->with = array(
				'country',
				'users',
		);
		$criteria->together = false;
		if($alias) {
			//$criteria->compare('category.alias', $alias);
			$criteria->addCondition('"category"."alias" = :alias');
			$criteria->params = array(':alias' => $alias);
			//$criteria->with = 'category';	
			$criteria->with = array(
				'country',
				'category',
				'users',
			);		
		}
		$session=new CHttpSession;
		$session->open();
		$currContry = $session['country'];
		$criteria->compare('country.name', $currContry);
		$criteria->addSearchCondition('title', $keyword, true);
		$criteria->addSearchCondition('content', $keyword, true,'OR');
		$criteria->order = "t.created DESC";
		$criteria->compare('is_published', 1, true);
		
		$dataProvider=new CActiveDataProvider('Article', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));
		
		//CVarDumper::dump($dataProvider->getData(),10,true);
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			
		));
	}
	public function actionCategorySub($categoryAlias = null, $subCategoryAlias = null)
	{
		$criteria=new CDbCriteria;
		$criteria->with = array(
			'country',
			'users',
		);
		$criteria->together = false;
		if($categoryAlias && $subCategoryAlias) {
			//$criteria->compare('category.alias', $alias);
			$criteria->addCondition('"category"."alias" = :categoryAlias');
			$criteria->addCondition('"sub_category"."alias" = :subCategoryAlias');
			$criteria->params = array(':subCategoryAlias' => $subCategoryAlias, ':categoryAlias' => $categoryAlias);
			//$criteria->with = 'category';	
			$criteria->with = array(
				'country',
				'category',
				'sub_category',
				'users',
			);		
		}
		$session=new CHttpSession;
		$session->open();
		$currContry = $session['country'];
		$criteria->compare('country.name', $currContry);
		$criteria->addSearchCondition('title', $keyword, true);
		$criteria->addSearchCondition('content', $keyword, true,'OR');
		$criteria->order = "t.created DESC";
		$criteria->compare('t.is_published', 1, true);
		
		$dataProvider=new CActiveDataProvider('Article', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));
		
		//CVarDumper::dump($dataProvider->getData(),10,true);
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			
		));
	}
	public function actionFollow()
	{
		// dump('1');
		$criteria=new CDbCriteria;
		$criteria->with = array('articles');
		$criteria->together  = true;
		$criteria->compare('articles.is_published', 1);
		$criteria->compare('t.user_id', currentUser()->id);
		$criteria->order = "articles.created DESC";
		// dump($criteria);
		$dataProvider=new CActiveDataProvider('Follows', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));
		
		
		$this->render('follow', array(
			'dataProvider'=>$dataProvider,
			'title'=>"My follow's articles"
		));
	}
	public function actionFlag()
	{
		// dump('1');
		$criteria=new CDbCriteria;
		$criteria->with = array('articles');
		$criteria->together  = true;
		$criteria->compare('articles.is_published', 1);
		$criteria->compare('t.user_id', currentUser()->id);
		$criteria->order = "articles.created DESC";
		// dump($criteria);
		$dataProvider=new CActiveDataProvider('Flags', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));
		
		
		$this->render('flag', array(
			'dataProvider'=>$dataProvider,
			'title'=>"My flag's articles"
		));
	}
	public function actionLike()
	{
		// dump('1');
		$criteria=new CDbCriteria;
		$criteria->with = array('articles');
		$criteria->together  = true;
		$criteria->compare('articles.is_published', 1);
		$criteria->compare('t.user_id', currentUser()->id);
		$criteria->order = "articles.created DESC";
		// dump($criteria);
		$dataProvider=new CActiveDataProvider('Likes', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));
		
		
		$this->render('like', array(
			'dataProvider'=>$dataProvider,
			'title'=>"My like's articles"
		));
	}
	public function actionContributed ()
	{
		// dump('1');
		$criteria=new CDbCriteria;
		$criteria->compare('user_id', currentUser()->id);
		$criteria->order = "created DESC";
		// dump($criteria);
		$dataProvider=new CActiveDataProvider('RevisionArticle', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));

		$this->render('contributed', array(
			'dataProvider'=>$dataProvider,
			'title'=>"My contributed's articles"
		));
	}
	public function actionMe()
	{
		
		$criteria=new CDbCriteria;
		$criteria->compare('is_published', 1);
		$criteria->compare('user_id', currentUser()->id);
		$criteria->order = "created DESC";
		
		$dataProvider=new CActiveDataProvider('Article', array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=> 10,
				'pageVar'=>'page',
			),
		));
		
		//CVarDumper::dump($dataProvider->getData(),10,true);
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			'title'=>'My articles'
		));
	}
	public function actionView($alias = null){
		if(empty($alias))
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
			
			
		$model = Article::model()->findByAttributes(array(
			'alias' => $alias
		));
		// dump($alias);
		if($model === null)
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
		
		$otherNews = Article::model()->findAll(array(
			'condition' => 'id<>:current_articles_id',
			'params' => array(
				':current_articles_id' => $model->id
			),
			'limit' => 5,
		));
		
		
		$criteria=new CDbCriteria;
		
		$criteria->compare('object_id', $model->id, true);
		$criteria->order = "created DESC";

		
		$pages = new CPagination(count(Comments::model()->findAll($criteria)));
		$pages->pageSize = 5;
		$pages->applyLimit($criteria);
		
		$comments = Comments::model()->findAll($criteria);
		
		$this->render('view', array(
				'model' => $model,
				'comments' => $comments,
				'pages' => $pages,
				'otherArticles' => $otherNews
		));
	}
	
	public function actionDelete($id) {
		$result = Article::model()->findByPk($id);
		if(!empty($result)){
			$result->delete();
			
			$likes = Likes::model()->findAllByAttributes(array('object_id'=>$id));
			if(!empty($likes)){
				foreach($likes as $key=>$value){
					$value->delete();
				}
			}
			$comment = Comments::model()->findAllByAttributes(array('object_id'=>$id));
			if(!empty($comment)){
				foreach($comment as $key=>$value){
					$value->delete();
				}
			}
			Yii::app()->user->setFlash('success', 'Article deleted successfully.');
		}else{
			Yii::app()->user->setFlash('error', 'Article deleted failer.');
		}
		
		$this->redirect(Yii::app()->request->urlReferrer);
		
	}
	
	public function actionChangeCountry() {
		$countryID = Yii::app()->request->getParam('country',null);
		$countryName = Country::model()->findByPk($countryID);
		
		$session=new CHttpSession;
		$session->open();
		$session['country'] = $countryName->name;
		$session->close();
		$this->redirect(Yii::app()->request->urlReferrer);
	}
}